//
//  CNHotView.m
//  BeeQuick_19
//
//  Created by whong7 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNHotView.h"
#import "CNHotViewModel.h"
#import "CNHotModel.h"
#import "JCTagListView.h"
#define filePath [NSHomeDirectory() stringByAppendingPathComponent:@"array.txt"]

@interface CNHotView ()
@property(nonatomic,strong)NSArray *buttonArray;

@property(nonatomic,strong)UIButton *cleanButton;
@property(nonatomic,strong)UILabel *historyLabel;
@property(nonatomic,strong)UIView *listView;
@end

@implementation CNHotView

#pragma mark - 程序入口
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setupUI];
    }
    return self;
}


-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
    
}
#pragma mark - 布局view
-(void)setupUI
{
    
    self.backgroundColor = [UIColor colorWithHex:0xefefef];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [self loadData];
    
}
#pragma mark - 加载数据+布局button

-(void)loadData
{
    __weak typeof(self) weakSelf = self;
    [CNHotViewModel loadDataWithBlock:^(CNHotModel *model) {
        if (model) {
            weakSelf.buttonArray = model.data[@"hotquery"];
            NSLog(@"%@",weakSelf.buttonArray);
            [weakSelf setupButton];
            //停止加载提示
            [SVProgressHUD dismiss];
        
        }
        else
        {
            NSLog(@"数据错误");
        }

    }];
}
#pragma mark - 布局button

-(void)setupButton
{
    //布局热门搜索
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10,20, 0, 0)];
    label.text = @"热门搜索";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:14];

    [label sizeToFit];
    [self addSubview:label];
    
    //布局标签
    JCTagListView *tagListView = [[JCTagListView alloc]initWithFrame:CGRectMake(0, 44, self.width, 120)];
    tagListView.backgroundColor = [UIColor colorWithHex:0xefefef];
    tagListView.tagBackgroundColor = [UIColor whiteColor];
    [self addSubview:tagListView];
    
    tagListView.canSelectTags = YES;
    tagListView.tagSelectedTextColor = [UIColor blackColor];
    tagListView.tagCornerRadius = 5.0f;

    [tagListView.tags addObjectsFromArray:self.buttonArray];
    
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",filePath);
    if (arr.count > 0) {
        //显示数据
        [self historyWithArray:arr.copy];
    }

    
    __weak typeof(self) weakSelf = self;
    [tagListView setCompletionBlockWithSelected:^(NSInteger index) {
         NSLog(@"______%ld______", (long)index);
        NSString *str = weakSelf.buttonArray[index];
        weakSelf.block(str);
        
        NSLog(@"%@",filePath);
        //对象解档
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (arr == nil) {
            arr = [[NSMutableArray alloc]init];
        }
        if (![arr containsObject:str]) {
            [arr insertObject:str atIndex:0];
        }
        
        //归档
        [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
  
    }];
    
}

-(void)historyWithArray:(NSArray *)array
{
    //布局热门搜索
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10,200, 0, 0)];
    label.text = @"历史记录";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:14];
    
    [label sizeToFit];
    self.historyLabel = label;
    [self addSubview:label];

    
    //布局标签
    JCTagListView *tagListView = [[JCTagListView alloc]initWithFrame:CGRectMake(0, 224, self.width, 120)];
    tagListView.backgroundColor = [UIColor colorWithHex:0xefefef];
    tagListView.tagBackgroundColor = [UIColor whiteColor];
    [self addSubview:tagListView];
    self.listView = tagListView;
    
    tagListView.canSelectTags = YES;
    tagListView.tagSelectedTextColor = [UIColor blackColor];
    tagListView.tagCornerRadius = 5.0f;
    
    [tagListView.tags addObjectsFromArray:array];
    __weak typeof(self) weakSelf = self;
    [tagListView setCompletionBlockWithSelected:^(NSInteger index) {
        NSLog(@"______%ld______", (long)index);
        NSString *str = array[index];
        weakSelf.block(str);
        
        NSLog(@"%@",filePath);
        //对象解档
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        if (arr == nil) {
            arr = [[NSMutableArray alloc]init];
        }
        if (![arr containsObject:str]) {
            [arr insertObject:str atIndex:0];
        }
        
        //归档
        [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
        
    }];
    
    //布局清空按钮
    UIButton *btn = [[UIButton alloc]init];
    
    //设置btn的内容 ：uinormal
    [btn setTitle:@"清除历史记录" forState:UIControlStateNormal];
    //设置btn的字体颜色
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    //设置背景
    btn.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    //添加监听
    [btn addTarget:self action:@selector(cleanClick) forControlEvents:UIControlEventTouchUpInside];
    self.cleanButton = btn;
    
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(40);
        make.width.offset(150);
    }];
    
}

-(void)cleanClick
{
    //归档清空
    [[NSFileManager defaultManager]removeItemAtPath:filePath error:nil];
    

    [self.historyLabel removeFromSuperview];
    [self.listView removeFromSuperview];
    [self.cleanButton removeFromSuperview];
    
}


@end
